package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    //查询活动列表
    @Override
    public Object list(Integer channel, Integer type, Integer status, Integer page, Integer pageSize) {
        //1.设置PageHelper参数分页
        PageHelper.startPage(page,pageSize);
        //2.调用Mapper方法进行查询
        List<Activity> activityList = activityMapper.list(channel,type,status);
        Page<Activity> p = (Page<Activity>)activityList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    //删除活动
    @Override
    public void deleteById(Integer id) {
        //调用Mapper方法进行删除
        activityMapper.deleteById(id);
    }
    //添加活动
    @Override
    public void add(Activity activity) {
        //补全时间属性
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法添加
        activityMapper.add(activity);
    }

    @Override
    public Object getById(Integer id) {
        return activityMapper.getById(id);
    }

    @Override
    public void update(Activity activity) {
        //补全时间属性
        activity.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法修改
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> listByType(Integer type) {
        //调用Mapper方法查询
         return activityMapper.listByType(type);
    }
}
